package cup.com.icss.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cup.com.icss.dao.TheAnnouncementMapper;
import cup.com.icss.model.TheAnnouncement;
import cup.com.icss.model.User;
import cup.com.icss.utils.LogUtil;
import cup.com.icss.utils.StringUtil;

@Controller
public class TheAnnouncementController {
	@Resource
	private TheAnnouncementMapper theAnnouncementMapper;
	
	@RequestMapping(value="/GetAnnouncementList",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> GetAnnouncementList(HttpServletRequest request,Integer page,Integer rows,TheAnnouncement record){
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			User user = (User)request.getSession().getAttribute("USER");
			record.setStoriedBuliding(user.getStoriedBuilding());
			if(page!=null && rows!=null){
				page=page==null?1:page;
				rows=rows==null?10:rows;
				int pageNum = page;
				int pageSize = rows;
				int num = theAnnouncementMapper.GetAnnouncementListSize(record).size();//需要更改（不用传数字） 传查询条件就可以
				int pageSizez;
				if(((pageSize*pageNum)-num)<0){
					pageSizez = pageSize;
				}else{
					pageSizez = num-(10*(pageNum-1));
				}
				List<TheAnnouncement> theAnnouncement = theAnnouncementMapper.GetAnnouncementList(pageNum*pageSize-pageSize,pageSizez,record);
				map.put("total", num);
				map.put("rows", theAnnouncement);
			}else{
				int num = theAnnouncementMapper.GetAnnouncementList(1,6,record).size();
				List<TheAnnouncement> theAnnouncement = theAnnouncementMapper.GetAnnouncementList(0,6,record);
				map.put("rows", theAnnouncement);
				map.put("total", num);
			}
		} catch (Exception e) {
			// TODO: handle exception
			LogUtil.debugLog(e);
		}
		return map;
	}
	
	@RequestMapping(value="/createAnnouncement",method=RequestMethod.GET)
	@ResponseBody
	public ModelAndView createAnnouncement(HttpServletRequest request,String storiedBuilding,String type){
		ModelAndView view = new ModelAndView();
		try {
			User user = (User)request.getSession().getAttribute("USER");
			view.addObject("storiedBuliding",storiedBuilding);
			view.addObject("type",type);
			view.setViewName("cup/jsp/announcement/announcementCreate");
		} catch (Exception e) {
			// TODO: handle exception
			LogUtil.errorLog(e);
		}
		return view;
	}
	
	@RequestMapping(value="/showAnnouncement",method=RequestMethod.GET)
	@ResponseBody
	public ModelAndView showAnnouncement(HttpServletRequest request,String id){
		ModelAndView view = new ModelAndView();
		try {
			TheAnnouncement theAnnouncement = theAnnouncementMapper.selectByPrimaryKey(id);
			view.addObject("title",theAnnouncement.getTitle());
			view.addObject("content",theAnnouncement.getContent());
			view.addObject("type",theAnnouncement.getType());
			view.setViewName("cup/jsp/announcement/announcementShow");
		} catch (Exception e) {
			// TODO: handle exception
			LogUtil.errorLog(e);
		}
		return view;
	}
	
	@RequestMapping(value="/addAnnouncements",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, String> addAnnouncements(HttpServletRequest request,TheAnnouncement record){
		Map<String, String> map = new HashMap<String,String>();
		try {
			

			Date date = new Date();
			User user = (User)request.getSession().getAttribute("USER");
			record.setId(StringUtil.getUUID());
			record.setReleasePeople(user.getCnName());
			record.setReleasePeopleId(user.getStudentId());
			record.setReleaseTime(date);
			int inster = theAnnouncementMapper.insertSelective(record);
			if(inster>0){
				map.put("status", "200");
			}else{
				map.put("status", "500");
			}
		} catch (Exception e) {
			// TODO: handle exception
			LogUtil.errorLog(e);
		}
		return map;
	}
	
	@RequestMapping(value="/announcementsList",method=RequestMethod.GET)
	@ResponseBody
	public ModelAndView announcementsList(HttpServletRequest request,String storiedBuilding){
		ModelAndView view = new ModelAndView();
		try {
			view.setViewName("cup/jsp/announcement/announcementList");
		} catch (Exception e) {
			// TODO: handle exception
			LogUtil.errorLog(e);
		}
		return view;
	}
	
	@RequestMapping(value="/announcementDelete",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, String> announcementDelete(HttpServletRequest request,String id){
		Map<String, String> map = new HashMap<String,String>();
		try {
			int delete = theAnnouncementMapper.deleteByPrimaryKey(id);
			if(delete>0){
				map.put("state", "200");
			}else{
				map.put("state", "500");
			}
		} catch (Exception e) {
			// TODO: handle exception
			LogUtil.errorLog(e);
		}
		return map;
	}
}
